package com.sskj.fca.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.fca.R;
import com.sskj.fca.bean.RechargeListBean;
import com.sskj.fca.presenter.RechargeRecordFragmentPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.APP_FRAGMENT_RECHARGE_RECORD)//充值记录
public class RechargeRecordFragment extends BaseFragment<RechargeRecordFragmentPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private MVCCoolHelper<List<RechargeListBean>> mvcHelper;
    private SlimAdapter slimAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_recharge_record;
    }

    @Override
    public RechargeRecordFragmentPresenter getPresenter() {
        return new RechargeRecordFragmentPresenter();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        refresh();
    }

    @Override
    public void initView() {
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_recharge_record, new SlimInjector<RechargeListBean>() {
            @Override
            public void onInject(RechargeListBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tv_recharge_number, data.getPrice() + " CNY")
                        .text(R.id.tv_recharge_state, data.getState().equals("1") ? "充币中" : "已到账")
                        .text(R.id.tv_recharge_date, data.getAddtime());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());

        ModelRx2DataSource<RechargeListBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<RechargeListBean>)
                        page -> mPresenter.getOpenAwardList(page, 10), 10);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setAutoLoadMore(true);
        mvcHelper.setNeedCheckNetwork(false);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        refresh();

//        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_recharge_record, new SlimInjector<RechargeListBean>() {
//            @Override
//            public void onInject(RechargeListBean data, IViewInjector injector, List payloads) {
//                injector.text(R.id.tv_recharge_address, data.getChongzhi_url())
//                        .text(R.id.tv_recharge_number, data.getPrice() + " USDT")
//                        .text(R.id.tv_recharge_state, data.getState().equals("1") ? "充币中" : "已到账")
//                        .text(R.id.tv_recharge_date, data.getAddtime());
//            }
//        }).attachTo(recyclerView).updateData(new ArrayList());
////        ModelRx2DataSource<String> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<String>)
////                page -> Flowable.range(0, 20)
////                        .map(i -> i + "")
////                        .toList().toFlowable(), 20);
//        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
//        ModelRx2DataSource<RechargeListBean> dataSource = new ModelRx2DataSource<>(
//                (ModelRx2DataSource.OnLoadSource<RechargeListBean>)
//                        page -> mPresenter.getOpenAwardList(page, 10), 10);
//        mvcHelper.setDataSource(dataSource);
//        mvcHelper.setAdapter(slimAdapter);
//        mvcHelper.refresh();
    }

    private void refresh() {
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }
}
